package cn.jixie.utils;

import cn.jixie.model.TextMessage;
import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: lzl
 * @Date: 18-10-24 10:13
 * @Description:
 * <xml> <ToUserName>< ![CDATA[toUser] ]></ToUserName> <FromUserName>< ![CDATA[fromUser] ]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType>< ![CDATA[text] ]></MsgType> <Content>< ![CDATA[你好] ]></Content> </xml>
 */
public class MessageUtils {

    /**
     *
     * 定义消息类型，之后可扩展
     */
    public static final String MESSAGE_TEXT = "text";

    /**
     * xml格式转换为map
     * @param request
     * @return map
     */
    public static Map<String,String> xmlToMap(HttpServletRequest request){
        Map<String ,String> map = new HashMap<String , String>();
        try {
            InputStream inputStream = request.getInputStream();
            SAXReader reader = new SAXReader();
            Document document = reader.read(inputStream);
            Element rootElement = document.getRootElement();
            List<Element> elements = rootElement.elements();
            for (Element element : elements){
                map.put(element.getName(),element.getText());
            }
            inputStream.close();
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    /**
     * 文本消息对象转为xml格式
     * @param textMessage
     * @return string
     */
    public static String textMessageToXml(TextMessage textMessage){
        XStream xStream = new XStream();
        xStream.alias("xml" , textMessage.getClass());
        return xStream.toXML(textMessage);
    }

    /**
     * 设置需要返回的文本信息
     * @param fromUserName
     * @param toUserName
     * @param content
     * @return
     */
    public static String initText(String fromUserName , String toUserName , String content){
        TextMessage text = new TextMessage();
        //注意接受消息和发送消息的顺序要烦过来
        text.setFromUserName(toUserName);
        text.setToUserName(fromUserName);
        text.setMsgType(MessageUtils.MESSAGE_TEXT);
        long time = System.currentTimeMillis();
        text.setCreateTime(String.valueOf(time));
        text.setContent(content);
        return MessageUtils.textMessageToXml(text);
    }
    /**
     * 设置订阅时，返回菜单的内容
     * @return
     */
    public static String menuText(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("菜单1：回复数字1，有惊喜\n");
        stringBuilder.append("菜单2：回复数字2，有惊喜\n");
        stringBuilder.append("菜单3：回复数字3，有惊喜\n");
        stringBuilder.append("菜单4：回复数字3，有惊喜\n");
        stringBuilder.append("菜单5：回复数字3，有惊喜\n");
        stringBuilder.append("菜单6：回复数字未知的东东，也还有有惊喜哦\n");
        return stringBuilder.toString();
    }


}
